package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

import ssm.entity.Employee;
import ssm.service.EmployeeService;

/**
 * @author lixiuming
 * @Description
 * @Date 2022年3月29日下午3:38:06
 */
@RequestMapping("/emp")
@Controller
public class EmployeeController {
	@Autowired
	private EmployeeService employeeService;

	/**
	 * 列表页面
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Employee employee) {
		ModelAndView mv = new ModelAndView("index");
		PageInfo<Employee> list = employeeService.list(employee);
		mv.addObject("pages", list);
		return mv;
	}

	/**
	 * 添加页面
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView("update");
		return mv;
	}

	/**
	 * 编辑页面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView edit(@PathVariable(name = "id", required = false) Integer id) {
		Employee employee = employeeService.getEmpById(id);
		ModelAndView mv = new ModelAndView("update");
		mv.addObject("employee", employee);
		return mv;
	}

	/**
	 * 添加/编辑
	 * 
	 * @param employee
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Employee employee) {
		System.out.println(employee);
		return employeeService.update(employee);
	}

	/**
	 * 详情
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = RequestMethod.POST)
	public Employee update(Integer id) {
		return employeeService.getEmpById(id);
	}

}
